11. CPU Usage
getCpuPercent()
5# Class ProcessParser - GetCpuPercent
Implementation
In order to create this function, you will parse data file data from /proc
.
Once we acquire the correct lines of data from proc/[PID]/stat
, we use it to calculate CPU usage. Because the timing is measured in CPU clock ticks, not seconds, we need to convert the data from the file.
We also need to get the process up time, system up time, and system host frequency.
string ProcessParser::getCpuPercent(string pid)
{
string line;
string value;
float result;
ifstream stream = Util::getStream((Path::basePath()+ pid + "/" + Path::statPath()));
getline(stream, line);
string str = line;
istringstream buf(str);
istream_iterator<string> beg(buf), end;
vector<string> values(beg, end); // done!
// acquiring relevant times for calculation of active occupation of CPU for selected process
float utime = stof(ProcessParser::getProcUpTime(pid));
float stime = stof(values[14]);
float cutime = stof(values[15]);
float cstime = stof(values[16]);
float starttime = stof(values[21]);
float uptime = ProcessParser::getSysUpTime();
float freq = sysconf(_SC_CLK_TCK);
float total_time = utime + stime + cutime + cstime;
float seconds = uptime - (starttime/freq);
result = 100.0*((total_time/freq)/seconds);
return to_string(result);
}